/*
 * @Author: 彭林
 * @Date: 2021-05-14 13:36:48
 * @LastEditTime: 2021-07-01 14:55:09
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/api/v1/campus.go
 */
package v1

import (
	"saas-app/app/define"
	"saas-app/app/model"
	"saas-app/app/service"
	"saas-app/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

func GetCampusList(r *ghttp.Request) {

	var (
		pagingParams *define.PagingParams
	)

	if err := r.Parse(&pagingParams); err != nil {
		panic(err)
	}

	result, total := service.CampusService().GetCampusList(r, pagingParams)

	response.JsonExit(
		r,
		0,
		"ok",
		response.Paging(result, total, pagingParams.Page, pagingParams.Limit),
	)
}

func GetCampusSimpleList(r *ghttp.Request) {
	result := service.CampusService().GetCampusSimpleList(r)
	response.JsonExit(r, 0, "ok", result)
}

func UpdateCampus(r *ghttp.Request) {

	var (
		data define.CampusUpdateRequest
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result := service.CampusService().UpdateCampus(r, r.GetFormMap())

	response.JsonExit(r, 0, "ok", result)
}

func CreateCampus(r *ghttp.Request) {

	var (
		schoolId = r.GetQueryUint("school_id")
		data     *define.CampusCreateRequest
		campus   *model.Campus
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if err := gconv.Struct(data, &campus); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	campus.SchoolId = schoolId

	if ok := service.CampusService().CreateCampus(campus); !ok {
		response.JsonExit(r, 0, "创建失败")
	} else {
		response.JsonExit(r, 0, "ok", "创建成功")
	}

}
